package cn.dugcrs.org.controller;

import cn.dugcrs.basic.anno.PreAuthorize;
import cn.dugcrs.basic.exception.BusinessException;
import cn.dugcrs.basic.utils.ExcelUtil;
import cn.dugcrs.org.domain.ShopAuditLog;
import cn.dugcrs.org.service.IShopService;
import cn.dugcrs.org.domain.Shop;
import cn.dugcrs.org.query.ShopQuery;
import cn.dugcrs.basic.utils.AjaxResult;
import cn.dugcrs.basic.utils.QueryResult;
import cn.dugcrs.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;


    /**
     * 不同状态的店铺数量报表原始数据
     */
    @GetMapping("/echarts")
    public Map<String,Object> getEchars(){
        Map<String, Object> map = shopService.loadEachars();
        return map;
    }

    /*
    审核驳回
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    审核通过
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 导出
     * @param response
     */
    @GetMapping("/export/excel")
    @PreAuthorize(name = "导出",value = "export:excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops,null,"店铺数据",Shop.class,"店铺信息.xls",response);
    }


    /**
     * 导入
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart(value = "file", required = true)MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file,0,1,Shop.class);
        shopService.saveAll(shops);

    }


    /* 
    * @Title: 店铺入驻
    * @Description: TODO
    * @Author: Gubcog
    * @Version: 1.0
    * @Date:  2022/5/4 16:41  
    * @Parameters: * @param shop
    * @Return cn.dugcrs.basic.utils.AjaxResult
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.success();
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }



    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return QueryResult 分页对象
    */
    @PostMapping("/list")
    public QueryResult<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
